import {
	getToken
} from '@/utils/auth'

// 登录页面
const loginPage = "/pages/login"

// 页面白名单
const whiteList = [
	'/pages/login',
	'/pages/register',
	'/pages/common/webview/index',
	// '/pages/goods/index',
	// '/pages/goods/goodsInfo/goodsInfo',
	'/pages/common/vips/vips',
	'/pages/mine/index',
	'/pages/index',
	'/pages/common/textview/index',
	'/pages/mine/recharge/recharge',
	'/pages/mine/order/orderRecord/orderRecord',
	'/pages/mine/about/index',
	'/pages/reservation/reservation',
	"/pages/common/confirmOrder/confirmOrder",
	"/pages/common/MapNavigation/MapNavigation",
	"/pages/common/wangqihuodong/wangqihuodong",
	"/pages/common/yuanzixiangqing/yuanzixiangqing",
	"/pages/common/vipCard/vipCard",
	"/pages/mine/help/index",
	"/pages/mine/yuanzishezi/yuanzishezi",
	"/pages/mine/vipList/vipList",
	'/'
]




// 检查地址白名单
function checkWhite(url) {
	const path = url.split('?')[0]
	console.log(path);
	let a = 0;
	for (let s of whiteList) {
		if (s.indexOf(path) !== -1) {
			a++;
			return true
		}
	}
	return a > 0
}

// 页面跳转验证拦截器
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]

list.forEach(item => {
	uni.addInterceptor(item, {
		invoke(to) {
			console.log(to);
			if (getToken()) {

				return true
			} else {
				if (checkWhite(to.url)) {
					return true
				}
				uni.showModal({
					title: '未登录',
					content: '该页面需要登录后访问,是否登录',
					success: function(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: loginPage
							})
						} else if (res.cancel) {
							console.log('用户点击取消');

						}
					}
				});


				return false
			}
		},
		fail(err) {
			console.log(err)
		}
	})
})